package com.ruoyi.web.controller.system;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;

import com.ruoyi.common.core.domain.Ztree;
import com.ruoyi.common.utils.ALiYunSms;
import com.ruoyi.common.utils.BaiDuSms;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.Customer;
import com.ruoyi.system.service.ICustomerService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.Member;
import com.ruoyi.system.domain.Integral;
import com.ruoyi.system.service.IIntegralService;
import com.ruoyi.system.service.IMemberService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 积分记录Controller
 * 
 * @author ruoyi
 * @date 2019-10-14
 */
@Controller
@RequestMapping("/system/integral")
public class IntegralController extends BaseController
{
    private String prefix = "system/integral";

    @Autowired
    private IIntegralService integralService;
    @Autowired
    private IMemberService memberService;
    @Autowired
    private ICustomerService customerService;

    @RequiresPermissions("system:integral:view")
    @GetMapping()
    public String integral()
    {
        return prefix + "/integral";
    }

    /**
     * 查询积分记录列表
     */
    @RequiresPermissions("system:integral:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Integral integral)
    {
        startPage();
        List<Integral> list = integralService.selectIntegralList(integral);
        for(Integral inte:list){
            if(inte.getCustomerId()!=null && !inte.getCustomerId().toString().equals("")){
                Customer customer = customerService.selectCustomerById(inte.getCustomerId());
                inte.setCustomerName(customer.getName());
            }
            if(inte.getMemberCid()!=null && !inte.getMemberCid().toString().equals("")){
                Member memeber = memberService.selectMemberById(inte.getMemberCid());
                inte.setMemberCName(memeber.getNickName());
            }
        }
        return getDataTable(list);
    }

    /**
     * 导出积分记录列表
     */
    @RequiresPermissions("system:integral:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Integral integral)
    {
        List<Integral> list = integralService.selectIntegralList(integral);
        ExcelUtil<Integral> util = new ExcelUtil<Integral>(Integral.class);
        return util.exportExcel(list, "integral");
    }
    
    /**
     * 新增积分记录
     */
    @GetMapping("/add/{id}")
    public String add(@PathVariable("id") Long id, ModelMap mmap)
    {
        Member member = memberService.selectMemberById(id);
        mmap.put("member", member);
        return prefix + "/add";
    }

    /**
     * 新增保存积分记录
     */
    @RequiresPermissions("system:integral:add")
    @Log(title = "积分记录", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Integral integral)
    {
    	Member selectMemberById = memberService.selectMemberById(integral.getMemberId());
        selectMemberById.setIntegralCount(selectMemberById.getIntegralCount()+integral.getIntegralCount());
        memberService.updateMember(selectMemberById);
        String method = "";
        int number = 0;
        if(integral.getIntegralCount()>=0){
            method = "增加";
            number = integral.getIntegralCount();
        }else{
            method = "减少";
            number = 0-integral.getIntegralCount();
        }
        try {
            ALiYunSms.sendSms(selectMemberById.getTelephone(), selectMemberById.getNickName(), method, number+"");
//            BaiDuSms.sendSms(selectMemberById.getTelephone(), selectMemberById.getNickName(), method, number+"");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return toAjax(integralService.insertIntegral(integral));
    }

    /**
     * 修改积分记录
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        Integral integral = integralService.selectIntegralById(id);
        mmap.put("integral", integral);
        return prefix + "/edit";
    }

    /**
     * 修改保存积分记录
     */
    @RequiresPermissions("system:integral:edit")
    @Log(title = "积分记录", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Integral integral)
    {
        return toAjax(integralService.updateIntegral(integral));
    }

    /**
     * 删除积分记录
     */
    @RequiresPermissions("system:integral:remove")
    @Log(title = "积分记录", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(integralService.deleteIntegralByIds(ids));
    }


    /**
     * 选择子业务员树
     */
    @GetMapping(value = { "/selectMemberTree/{id}", "/selectMemberTree/" })
    public String selectMemberTree(@PathVariable(value = "id", required = false) Long id, ModelMap mmap)
    {
        if (StringUtils.isNotNull(id))
        {
            mmap.put("member", memberService.selectMemberById(id));
        }
        return prefix + "/member_tree";
    }

    /**
     * 加载子业务员树列表
     */
    @GetMapping("/memberTreeData")
    @ResponseBody
    public List<Ztree> memberTreeData()
    {
        List<Ztree> ztrees = memberService.selectMemberTree();
        return ztrees;
    }

    /**
     * 选择客户树
     */
    @GetMapping(value = { "/selectCustomerTree/{id}", "/selectCustomerTree/" })
    public String selectCustomerTree(@PathVariable(value = "id", required = false) Long id, ModelMap mmap)
    {
        if (StringUtils.isNotNull(id))
        {
            mmap.put("customer", customerService.selectCustomerById(id));
        }
        return prefix + "/customer_tree";
    }

    /**
     * 加载客户树列表
     */
    @GetMapping("/customerTreeData")
    @ResponseBody
    public List<Ztree> customerTreeData()
    {
        List<Ztree> ztrees = customerService.selectCustomerTree();
        return ztrees;
    }


}
